package gupao.concurrency.threads;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicBoolean;

public class AtomicBooleanSample {

    static class MultiThreadBooleanFlag{
        private final AtomicBoolean hasError = new AtomicBoolean(false);
        private Runnable multiThreadTask = () -> {
            try{
                doBizLogic();
            }catch (Throwable t){
                hasError.set(true);
            }
        };

        private void doBizLogic(){
        }
    }

    @Slf4j
    static class InitOnce{

        final AtomicBoolean initialized = new AtomicBoolean(false);
        @PostConstruct
        public void init(){
            if(initialized.compareAndSet(false, true)){
                //do init
            }
            log.info("xxx initialized.");
        }
    }

    @Slf4j
    static class SingleTask{
        final AtomicBoolean started = new AtomicBoolean(false);
        public void start(){
            if(started.compareAndSet(false, true)){
                //start a single task
            }
            log.info("xxx task started.");
        }
    }
}
